package Thread_;

public class SellTicket {
    public static void main(String[] args) {

        SellCard sellCard0 = new SellCard();
        new Thread(sellCard0).start();
        new Thread(sellCard0).start();
        new Thread(sellCard0).start();
    }
}

class SellCard implements Runnable {
    private static int TicketNum = 50;
    private boolean loop = true;

    public /*synchronized*/ void sell() {
        synchronized (this) {
            if (TicketNum <= 0) {
                System.out.println("售票结束...");
                loop = false;
                return;
            }

            System.out.println("窗口" + Thread.currentThread().getName() + "售出一张" + "剩余票数=" + (--TicketNum) + "张");
        }
    }


    @Override
    public void run() {
        while (loop) {
            sell();
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}